package gold.digger;

import gold.utils.InputUtil;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC718 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {

        public int findLength(int[] A, int[] B) {
            int[][] dp = new int[A.length][B.length];
            for (int i = 0; i < A.length; i++) {
                if (A[i] == B[0]) dp[i][0] = 1;
            }
            for (int j = 0; j < B.length; j++) {
                if (A[0] == B[j]) dp[0][j] = 1;
            }

            int maxLen = 0;
            for (int i = 1; i < A.length; i++) {
                for (int j = 1; j < B.length; j++) {
                    if (A[i] == B[j]) {
                        dp[i][j] = dp[i - 1][j - 1] + 1;
                    } else {
                        dp[i][j] = 0;
                    }
                    maxLen = Math.max(maxLen, dp[i][j]);
                }
            }

            return maxLen;
        }

        public int findLengthIgnoreConsecutive(int[] A, int[] B) {
            int[][] dp = new int[A.length][B.length];
            for (int i = 0; i < A.length; i++) {
                if (A[i] == B[0]) dp[i][0] = 1;
            }
            for (int j = 0; j < B.length; j++) {
                if (A[0] == B[j]) dp[0][j] = 1;
            }

            for (int i = 1; i < A.length; i++) {
                for (int j = 1; j < B.length; j++) {
                    if (A[i] == B[j]) {
                        dp[i][j] = dp[i - 1][j - 1] + 1;
                    } else {
                        dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                    }
                }
            }

            return dp[A.length - 1][B.length - 1];
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr1 = InputUtil.toIntegerArray("[0,1,1,1,1]");
        int[] arr2 = InputUtil.toIntegerArray("[1,0,1,0,1]");
        System.out.println(solution.findLength(arr1, arr2));
    }

    public static void main(String[] args) throws Exception {
        LC718 an = new LC718();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}


